﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using WebService.VO;

namespace WebService.DAO
{
    public class FavoriteDAO
    {
        private dbConnection conn;
        /// <summary>
        /// khởi tạo đối tượng xử lý dl
        /// </summary>
        public FavoriteDAO()
        {
            conn = new dbConnection();
        }
        /// <summary>
        /// get favorite item list by device id
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public DataTable getFavoriteList(DeviceVO device, string language)
        {
            if (language.Equals("vi"))
            {
                return conn.GetDataTable("Select a.[ItemID],[ItemName],[CategoryID],[Rate],[Address],[Phone],[Email],[Note],[X],[Y],[Image] From tbl_Favorite a, tbl_Items b Where a.ItemID = b.ItemID AND DeviceID=" + device.Id);
            }
            return conn.GetDataTable("Select a.[ItemID],b.[ItemName] as ItemName,[CategoryID],[Rate],b.[Address] as Address,[Phone],[Email],b.[Note] as Note,[X],[Y],[Image] From tbl_Items a, tbl_ItemsTranslation b, tbl_Favorite c Where a.ItemID=b.ItemID And a.ItemID = c.ItemID And Language='" + language + "' AND DeviceID=" + device.Id);
        }

        /// <summary>
        /// check item existing in user favorite item list
        /// </summary>
        /// <param name="favorite"></param>
        /// <returns></returns>
        public bool check(FavoriteVO favorite)
        {
            string sqlSelect = "Select count(*) From tbl_Favorite Where DeviceID=@DeviceID and ItemID=@ItemID";
            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@DeviceID", favorite.Device.Id.ToString());
            d.Add("@ItemID", favorite.Item.ItemId.ToString());

            int result = conn.ExecuteQuery(sqlSelect, d, false);
            if (result == 0)
                return false;
            return true;
        }

        /// <summary>
        /// add item to user favorite list
        /// </summary>
        public void insert(FavoriteVO favorite)
        {
            string sqlInsert = "INSERT INTO tbl_Favorite VALUES(@ItemID, @DeviceID)";

            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@DeviceID", favorite.Device.Id.ToString());
            d.Add("@ItemID", favorite.Item.ItemId.ToString());

            conn.ExecuteQuery(sqlInsert, d, false);

        }

        /// <summary>
        /// delete item from user favorite list
        /// </summary>
        public void delete(FavoriteVO favorite)
        {
            string sql = "DELETE FROM tbl_Favorite WHERE ItemID=@ItemId AND DeviceID=@DeviceId";

            IDictionary<string, string> d = new Dictionary<string, string>();

            d.Add("@ItemId", favorite.Item.ItemId.ToString());
            d.Add("@DeviceId", favorite.Device.Id.ToString());


            conn.ExecuteQuery(sql, d, false);
        }
        
    }
}